<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Form newline normalization</title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
  </head>
  <body>
    <script>
      function createForm(testCase, name, value) {
        const form = document.createElement("form");
        const input = document.createElement("input");
        input.type = "hidden";
        input.name = name;
        input.value = value;
        form.appendChild(input);
        document.body.appendChild(form);
        testCase.add_cleanup(() => {
          document.body.removeChild(form);
        });
        return form;
      }

      function createFormWithFile(testCase, name, filename) {
        const form = document.createElement("form");
        const input = document.createElement("input");
        input.type = "file";
        input.name = name;
        const dataTransfer = new DataTransfer();
        dataTransfer.items.add(new File([], filename, { type: "text/plain" }));
        input.files = dataTransfer.files;
        form.appendChild(input);
        document.body.appendChild(form);
        testCase.add_cleanup(() => {
          document.body.removeChild(form);
        });
        return form;
      }

      test((testCase) => {
        const formData = new FormData(createForm(testCase, "a", "b\nc"));
        assert_equals(formData.get("a"), "b\r\nc");
      }, document.title + ": \\n in the value becomes \\r\\n");

      test((testCase) => {
        const formData = new FormData(createForm(testCase, "a", "b\rc"));
        assert_equals(formData.get("a"), "b\r\nc");
      }, document.title + ": \\r in the value becomes \\r\\n");

      test((testCase) => {
        const formData = new FormData(createForm(testCase, "a", "b\r\nc"));
        assert_equals(formData.get("a"), "b\r\nc");
      }, document.title + ": \\r\\n in the value stays unchanged");

      test((testCase) => {
        const formData = new FormData(createForm(testCase, "a", "b\n\rc"));
        assert_equals(formData.get("a"), "b\r\n\r\nc");
      }, document.title + ": \\n\\r in the value becomes \\r\\n\\r\\n");

      test((testCase) => {
        const formData = new FormData(createForm(testCase, "a\nb", "c"));
        assert_equals([...formData][0][0], "a\r\nb");
      }, document.title + ": \\n in the name becomes \\r\\n");

      test((testCase) => {
        const formData = new FormData(createForm(testCase, "a\rb", "c"));
        assert_equals([...formData][0][0], "a\r\nb");
      }, document.title + ": \\r in the name becomes \\r\\n");

      test((testCase) => {
        const formData = new FormData(createForm(testCase, "a\r\nb", "c"));
        assert_equals([...formData][0][0], "a\r\nb");
      }, document.title + ": \\r\\n in the name stays unchanged");

      test((testCase) => {
        const formData = new FormData(createForm(testCase, "a\n\rb", "c"));
        assert_equals([...formData][0][0], "a\r\n\r\nb");
      }, document.title + ": \\n\\r in the name becomes \\r\\n\\r\\n");

      test((testCase) => {
        const formData = new FormData(
          createFormWithFile(testCase, "a", "b\nc")
        );
        assert_equals(formData.get("a").name, "b\nc");
      }, document.title + ": \\n in the filename stays unchanged");

      test((testCase) => {
        const formData = new FormData(
          createFormWithFile(testCase, "a", "b\rc")
        );
        assert_equals(formData.get("a").name, "b\rc");
      }, document.title + ": \\r in the filename stays unchanged");

      test((testCase) => {
        const formData = new FormData(
          createFormWithFile(testCase, "a", "b\r\nc")
        );
        assert_equals(formData.get("a").name, "b\r\nc");
      }, document.title + ": \\r\\n in the filename stays unchanged");

      test((testCase) => {
        const formData = new FormData(
          createFormWithFile(testCase, "a", "b\n\rc")
        );
        assert_equals(formData.get("a").name, "b\n\rc");
      }, document.title + ": \\n\\r in the filename stays unchanged");
    </script>
  </body>
</html>
